package com.zmx.common.common.utils.Lock;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 基于Redisson分布式锁
 * @ClassName: RedissonLock
 * @Author zhaomxr
 * @Date 2021-10-18 15:59
 */
@Component
public class RedissonLock {

    private RedissonClient redissonClient;

    @Autowired
    public void RedissonLock(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    /**
     * 获取锁
     *
     * @param key
     * @return
     */
    public RLock lock(String key) {
        return redissonClient.getLock(key);
    }

    /**
     * 释放锁
     *
     * @param lock
     */
    public void unLock(RLock lock) {
        lock.unlock();
    }

    /**
     * 批量释放锁
     *
     * @param locks
     */
    public void unLock(List<RLock> locks) {
        for (RLock lock : locks) {
            this.unLock(lock);
        }
    }

}
